<?php
namespace App\Models\AdminModels;
use App\Models\ParentModel;
use DB;
/**
 * 消息model
 * @author puppet
 */
class Message extends ParentModel{
  /**
   * 与模型关联的数据表。
   *
   * @var string
   */
  protected $table = 'pt_message';

  /**
   * DB::直接操作的数据库表名
   * @var string
   */
  protected $db_table = 'pt_message';

  /**
   * 主键 默认id。
   *
   * @var string
   */
  protected $primaryKey = 'id';

  /**
   * 指定是否模型应该被戳记时间。
   *
   * @var bool
   */
  public $timestamps = false;
  
  /**
   * 翻页展示所有信息
   */
  public function getPageData($params){
    $res = false;
    $limit_num = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
    $limit_sta = 0;
    $page = 1;
    if (isset($params['page']) && intval($params['page']) > 0) {
      $limit_sta = intval($limit_num * intval($params['page'] - 1));
      $page = intval($params['page']);
    }
    $cou_sql = "SELECT count(*) as count FROM {$this->db_table} WHERE `status` >= ?";
    $sql = "SELECT id,title,content,send_name,admin_id,receive_ids,status,create_time,update_time,type FROM {$this->db_table} WHERE `status` >= ?";
    $values = [0];
    if(isset($params['title']) && !empty($params['title'])){
      $cou_sql .= " AND title like '%".$params['title']."%'";
      $sql .= " AND title like '%".$params['title']."%'";
    }
    if(isset($params['start_date']) && !empty($params['start_date'])){
      $start_date = $params['start_date'].' 00:00:00';
      $cou_sql .= " AND create_time >= ?";
      $sql .= " AND create_time >= ?";
      $values[] = $start_date;
    }
    if(isset($params['end_date']) && !empty($params['end_date'])){
      $end_date = $params['end_date'].' 23:59:59';
      $cou_sql .= " AND create_time <= ?";
      $sql .= " AND create_time <= ?";
      $values[] = $end_date;
    }
    $count = DB::select($cou_sql,$values);
    $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
    if(isset($params['order']) && !empty($params['order'])){
      $order_db = ['id'];
      $sql .= " ORDER BY ";
      foreach ($params['order'] as $order){
        $result = isset($order_db[$order['column']]) ? $order_db[$order['column']] : '';
        !empty($result) && $sql .= $result.' '.$order['dir'].',';
      }
      $sql = substr($sql,0,-1);
    }
    $sql .= " LIMIT $limit_sta,$limit_num";
    $data = DB::select($sql, $values);
    if(!empty($data)){
      $oAdmin = new Admin();
      foreach ($data as $key => $val){
          if($val['type']==1){
              $data[$key]['type']='合伙人';
          }elseif($val['type']==2){
              $data[$key]['type']='会员';
          }elseif (($val['type']==3)){
              $data[$key]['type']='好友推';
          }elseif (($val['type']==4)){
              $data[$key]['type']='总后台';
          }else{
              $data[$key]['type']='全部';
          }
        if($val['status']==1){
            $data[$key]['status']="开启";
        }else{
            $data[$key]['status']="关闭";
        }
        $adminData = $oAdmin->db_get(['admin_id'=>['=',$val['admin_id']]]);
       //   print_r($adminData);exit();
          $data[$key]['admin_name'] = !empty($adminData['admin_name']) ? $adminData['admin_name'] : '';

      }
    }
    $res['data'] = $data;
    $res['total'] = $count;
    //总条数记录
    return $res;
  }
     //接口获取message

    public function getAppMseeage($params){
        $res = false;
        $limit_num = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
        $limit_sta = 0;
        $type=$params['type'];
        $page = 1;
        if (isset($params['page']) && intval($params['page']) > 0) {
            $limit_sta = intval($limit_num * intval($params['page'] - 1));
            $page = intval($params['page']);
        }
        $cou_sql = "SELECT count(*) as count FROM {$this->db_table} WHERE `status` >= ? and type='$type' or type=0 ";
        $sql = "SELECT id,title,content,send_name,status,create_time FROM {$this->db_table} WHERE `status` >= ? and type='$type' or type=0 ";
        $values = [0];
        if(isset($params['title']) && !empty($params['title'])){
            $cou_sql .= " AND title like '%".$params['title']."%'";
            $sql .= " AND title like '%".$params['title']."%'";
        }
        if(isset($params['start_date']) && !empty($params['start_date'])){
            $start_date = $params['start_date'].' 00:00:00';
            $cou_sql .= " AND create_time >= ?";
            $sql .= " AND create_time >= ?";
            $values[] = $start_date;
        }
        if(isset($params['end_date']) && !empty($params['end_date'])){
            $end_date = $params['end_date'].' 23:59:59';
            $cou_sql .= " AND create_time <= ?";
            $sql .= " AND create_time <= ?";
            $values[] = $end_date;
        }
        $count = DB::select($cou_sql,$values);
        $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
        if(isset($params['order']) && !empty($params['order'])){
            $order_db = ['id'];
            $sql .= " ORDER BY ";
            foreach ($params['order'] as $order){
                $result = isset($order_db[$order['column']]) ? $order_db[$order['column']] : '';
                !empty($result) && $sql .= $result.' '.$order['dir'].',';
            }
            $sql = substr($sql,0,-1);
        }
        $sql .= " LIMIT $limit_sta,$limit_num";
        $data = DB::select($sql, $values);
          $res['data'] = $data;
        $res['total'] = $count;
        //总条数记录
        return $res;
    }
    /*
       * 获取消息详情的方法
       * */
    public function messageInfo($id){
        $sql=" select title,content,send_name,admin_id,create_time,update_time,receive_ids from pt_message where status=1";
        $sql.=" and id = ?";
        $re=DB::select($sql,[$id]);
   //  print_r($re);exit();
        $html="";
        if($re){
            $html.="<div class='modal-body'>";
            $html.="<label class='col-sm-4 control-label'>标题：</label><p>{$re[0]['title']}</p>";
            $html.="<label class='col-sm-4 control-label'>内容：</label><div class='mytc'><p>{$re[0]['content']}</p></div>";
            $html.="<label class='col-sm-4 control-label'>发送者：</label><p>{$re[0]['send_name']}</p>";
            $html.="<label class='col-sm-4 control-label'>创建者：</label><p>{$re[0]['admin_id']}</p>";
            $html.="<label class='col-sm-4 control-label'>发送时间：</label><p>{$re[0]['create_time']}</p>";
            $html.="<label class='col-sm-4 control-label'>接收者编号：</label><div class='mytc'><p>{$re[0]['receive_ids']}</p></div>";
            $html.="</div>";
        }
        return $html;
    }
}